package com.clps.cn.ActiviteDay05;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class InclusiveGateWayTest {

    /**
     * 若干个脚本进程创建，
     */
    @Test
    public void test001() {
        //01得到processEngin
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        //02得到ResitoryService对象
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //03部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday05_03.bpmn")
                .name("请假流程-流程变量")
                .deploy();
        System.out.println("deploy.getId() = " + deploy.getId());
        System.out.println("deploy.getName() = " + deploy.getName());
    }



    //启动流程实例，同时还要设置流程变量的值
    // act_ge_bytearray
    // act_ru_variable
    @Test
    public void test01() {
        //调用ProcessEngin进程
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //02:得到ResitoryService对象
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //03：流程定义的key问题，myProcess_1
        String key = "inclusiveGateway";
        Integer userType = 2;
        Map<String, Object> map = new HashMap<>();
//
//        Holiday holiday = new Holiday();
//        holiday.setNum(5F);
        map.put("userType", userType);

        //04启动流程实例，并设置流程中的变量的值
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, map);

        //05输出实例信息
        System.out.println(processInstance.getName());
        System.out.println(processInstance.getProcessDefinitionId());
    }


    //完成任务 zhansgan --lisi --判断请假的天数，进行分支
    @Test
    public void testzhangsanComplateTask() {

        //01得到ProcessEngin对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //02得到taskService
        TaskService taskService = processEngine.getTaskService();
        //03:查询当前是否有任务
        String key = "inclusiveGateway";
        Task task = taskService.createTaskQuery().processDefinitionKey(key)
                .taskAssignee("zhangsan").singleResult();
        //04判断任务是否存在
        System.out.println("task.getId() = " + task.getId());
        if (!Objects.isNull(task)) {
            taskService.complete(task.getId());
            System.out.println("task.getId() = " + task.getId());
        }
    }

    //完成任务 zhansgan --lisi --判断请假的天数，进行分支
    @Test
    public void testLisiComplateTask() {

        //01得到ProcessEngin对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //02得到taskService
        TaskService taskService = processEngine.getTaskService();
        //03:查询当前是否有任务
        String key = "inclusiveGateway";
        Task task = taskService.createTaskQuery().processDefinitionKey(key)
                .taskAssignee("lisi").singleResult();
        //04判断任务是否存在

        System.out.println("task.getId() = " + task.getId());

        if (!Objects.isNull(task)) {
            taskService.complete(task.getId());
            System.out.println("task.getId() = " + task.getId());
        }
    }


    //完成任务 总经理审批，赵六 --lisi --判断请假的天数，进行分支
    @Test
    public void testLisiComplateTaskwangwu() {

        //01得到ProcessEngin对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //02得到taskService
        TaskService taskService = processEngine.getTaskService();
        //03:查询当前是否有任务
        String key = "inclusiveGateway";
        Task task = taskService.createTaskQuery().processDefinitionKey(key)
                .taskAssignee("wangwu").singleResult();
        //04判断任务是否存在

        System.out.println("task.getId() = " + task.getId());

        if (!Objects.isNull(task)) {
            taskService.complete(task.getId());
            System.out.println("task.getId() = " + task.getId());
        }
    }


    /**
     * 人事经理存档
     */
    @Test
    public void testLisiComplateTaskzhaoliu() {

        //01得到ProcessEngin对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //02得到taskService
        TaskService taskService = processEngine.getTaskService();
        //03:查询当前是否有任务
        String key = "inclusiveGateway";
        Task task = taskService.createTaskQuery().processDefinitionKey(key)
                .taskAssignee("zhaoliu").singleResult();
        //04判断任务是否存在

        System.out.println("task.getId() = " + task.getId());

        if (!Objects.isNull(task)) {
            taskService.complete(task.getId());
            System.out.println("task.getId() = " + task.getId());
        }
    }

    /**
     * 人事经理存档
     */
    @Test
    public void testLisiComplateTaskxiaohong() {

        //01得到ProcessEngin对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //02得到taskService
        TaskService taskService = processEngine.getTaskService();
        //03:查询当前是否有任务
        String key = "inclusiveGateway";
        Task task = taskService.createTaskQuery().processDefinitionKey(key)
                .taskAssignee("小红").singleResult();
        //04判断任务是否存在

        System.out.println("task.getId() = " + task.getId());

        if (!Objects.isNull(task)) {
            taskService.complete(task.getId());
            System.out.println("task.getId() = " + task.getId());
        }
    }
}
